package com.qlslylq.ad.example.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.qlslylq.ad.example.R;
import com.qlslylq.ad.example.model.NormalItem;
import com.qlslylq.ad.sdk.core.ad.SingleFeedAd;

import java.util.HashMap;
import java.util.List;

/**
 * created by qlslylq <br/>
 * on 2022/2/16 <br/>
 */
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.CustomViewHolder> {

    private static final int TYPE_DATA = 0;
    private static final int TYPE_AD = 1;

    private Context context;
    private List<Object> list;
    private HashMap<SingleFeedAd, Integer> adPosMap;

    public FeedAdapter(Context context, List list, HashMap<SingleFeedAd, Integer> adPosMap) {
        this.context = context;
        this.list = list;
        this.adPosMap = adPosMap;
    }

    public List<Object> getList() {
        return list;
    }

    public void addAdToPosition(int position, SingleFeedAd ad) {
        if (position >= 0 && position < list.size() && ad != null) {
            list.add(position, ad);
        }
    }

    public void removeAD(int position, SingleFeedAd ad) {
        list.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(0, list.size() - 1);
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position) instanceof SingleFeedAd ? TYPE_AD : TYPE_DATA;
    }

    @Override
    public void onBindViewHolder(final CustomViewHolder customViewHolder, final int position) {
        int type = getItemViewType(position);
        if (type == TYPE_AD) {
            final SingleFeedAd ad = (SingleFeedAd) list.get(position);
            View adView = ad.getAdView(context);
            adPosMap.put(ad, position);
            if (customViewHolder.container.getChildCount() > 0
                    && customViewHolder.container.getChildAt(0) == adView) {
                return;
            }

            if (customViewHolder.container.getChildCount() > 0) {
                customViewHolder.container.removeAllViews();
            }

            if (adView.getParent() != null) {
                ((ViewGroup) adView.getParent()).removeView(adView);
            }

            customViewHolder.container.addView(adView);
            ad.render(customViewHolder.container);
        } else {
            customViewHolder.title.setText(((NormalItem) list.get(position)).getTitle());
        }
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        int layoutId = (viewType == TYPE_AD) ? R.layout.list_item_feed_ad : R.layout.list_item_feed_normal;
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, null);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }

    class CustomViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public ViewGroup container;

        public CustomViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.tv_title);
            container = (ViewGroup) view.findViewById(R.id.feed_ad_container);
        }
    }
}
